home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / graphics / a-g / amountains / a_graphics.c next >
C/C++ Source or Header  |  1996-01-14  |  4KB  |  191 lines

  1. #include <intuition/intuition.h>
  2.  
  3. #include <graphics/gfx.h>
  4. #include <graphics/gfxmacros.h>
  5. #include <graphics/modeid.h>
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/intuition.h>
  10. #include <proto/graphics.h>
  11.  
  12. #include <math.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #include "paint.h"
  17.  
  18. extern void finish_artist( void );
  19.  
  20. struct Color {
  21.     ULONG pen;
  22.     ULONG red;
  23.     ULONG green;
  24.     ULONG blue;
  25. };
  26.  
  27. static void finish_graphics( void );
  28.  
  29. static struct Screen    *scr;
  30. static struct Window    *win;
  31. static struct RastPort    *rp;
  32. static struct Color        *table;
  33. static int                cols;
  34. static int                graph_width;
  35. static int                graph_height;
  36. static UWORD            pens[] = { (UWORD) ~0 };
  37.  
  38. int quit_xmount    = FALSE;
  39.  
  40. void plot_pixel( int x, int y, Gun value )
  41. {
  42.     if ( x > 0 ) {
  43.         SetAPen( rp, table[value].pen );
  44.         WritePixel( rp, x, y );
  45.     }
  46. }
  47.  
  48. extern ULONG ModeID( char * );
  49.  
  50. void init_graphics(
  51.     int        want_use_root,
  52.     int        *s_graph_width,
  53.     int        *s_graph_height,
  54.     int        ncol,
  55.     Gun        *red,
  56.     Gun        *green,
  57.     Gun        *blue,
  58.     char    *modeid,
  59.     ULONG    depth,
  60.     char    *pubscreen
  61. )
  62. {
  63.     int i;
  64.  
  65.     table = (struct Color *) malloc( ncol * sizeof( struct Color ) );
  66.     if ( table == NULL ) {
  67.         PutStr( "malloc failed for colour table\n" );
  68.         exit( 1 );
  69.     }
  70.  
  71.     if ( modeid && depth ) {
  72.         scr = OpenScreenTags(
  73.             NULL,
  74.             SA_Width,        STDSCREENWIDTH,
  75.             SA_Height,        STDSCREENHEIGHT,
  76.             SA_Title,        "AMountains",
  77.             SA_Depth,        depth,
  78.             SA_Type,        CUSTOMSCREEN,
  79.             SA_DisplayID,    ModeID( modeid ),
  80.             SA_Overscan,    OSCAN_STANDARD,
  81.             SA_Pens,        pens,
  82.             SA_SysFont,        1,
  83.             SA_SharePens,    TRUE,
  84.             TAG_DONE
  85.         );
  86.     }
  87.  
  88.     win = OpenWindowTags(
  89.         NULL,
  90.         WA_Title,                want_use_root ? NULL : "AMountains",
  91.         WA_InnerWidth,            *s_graph_width,
  92.         WA_InnerHeight,            *s_graph_height,
  93.         WA_Backdrop,            want_use_root,
  94.         WA_Borderless,            want_use_root,
  95.         scr ? TAG_IGNORE : WA_PubScreenName,    pubscreen,
  96.         scr ? WA_CustomScreen : TAG_IGNORE,        scr,
  97.         WA_PubScreenFallBack,    TRUE,
  98.         WA_AutoAdjust,            TRUE,
  99.         WA_Flags,                WFLG_NOCAREREFRESH                        |
  100.                                 WFLG_GIMMEZEROZERO                        |
  101.                                 WFLG_ACTIVATE                            |
  102.                                 (want_use_root ? 0 : WFLG_CLOSEGADGET)    |
  103.                                 WFLG_DEPTHGADGET                        |
  104.                                 WFLG_DRAGBAR,
  105.         WA_IDCMP,                IDCMP_CLOSEWINDOW        |
  106.                                 IDCMP_ACTIVEWINDOW        |
  107.                                 IDCMP_INACTIVEWINDOW,
  108.         TAG_DONE
  109.     );
  110.     if ( win == NULL ) {
  111.         PutStr( "Window did not open\n" );
  112.         finish_graphics();
  113.         exit( 1 );
  114.     }
  115.  
  116.     graph_width  = *s_graph_width  = win->GZZWidth;
  117.     graph_height = *s_graph_height = win->GZZHeight;
  118.  
  119.     rp = win->RPort;
  120.  
  121.     for ( i = 0; i < ncol; i++ ) {
  122.         table[i].red    = (red[i]   << 16) | red[i];
  123.         table[i].green    = (green[i] << 16) | green[i];
  124.         table[i].blue    = (blue[i]  << 16) | blue[i];
  125.  
  126.         table[i].pen = ObtainBestPen(
  127.             win->WScreen->ViewPort.ColorMap,
  128.             table[i].red, table[i].green, table[i].blue,
  129.             OBP_Precision,    PRECISION_EXACT,
  130.             OBP_FailIfBad,    FALSE,
  131.             TAG_DONE
  132.         );
  133.  
  134.         if ( table[i].pen == -1 ) {
  135.             finish_graphics();
  136.             Printf( "failed to allocate colour %ld\n", i );
  137.             exit( 1 );
  138.         }
  139.         cols++;
  140.     }
  141.  
  142.     SetBPen( rp, table[SKY].pen );
  143.     SetRast( rp, table[SKY].pen );
  144. }
  145.  
  146. LONG activepri, inactivepri;
  147.  
  148. void zap_events( int snooze )
  149. {
  150.     struct IntuiMessage *imsg;
  151.  
  152.     if ( imsg = (struct IntuiMessage *) GetMsg( win->UserPort ) ) {
  153.         switch ( imsg->Class ) {
  154.             case IDCMP_ACTIVEWINDOW:
  155.                 SetTaskPri( FindTask( NULL ), activepri );
  156.                 break;
  157.             case IDCMP_INACTIVEWINDOW:
  158.                 SetTaskPri( FindTask( NULL ), inactivepri );
  159.                 break;
  160.             case IDCMP_CLOSEWINDOW:
  161.                 quit_xmount = TRUE;
  162.                 break;
  163.         }
  164.         ReplyMsg( (struct Message *) imsg );
  165.     }
  166.     if ( quit_xmount ) {
  167.         finish_graphics();
  168.         finish_artist();
  169.         exit( 0 );
  170.     }
  171.     if ( snooze ) {
  172.         Delay( 50 * snooze );
  173.     }
  174. }
  175.  
  176. void scroll_screen( int dist )
  177. {
  178.     ScrollRaster( rp, dist, 0, 0, 0, graph_width, graph_height );
  179. }
  180.  
  181. static void finish_graphics( void )
  182. {
  183.     int i;
  184.  
  185.     for ( i = 0; i < cols; i++ ) {
  186.         ReleasePen( win->WScreen->ViewPort.ColorMap, table[i].pen );
  187.     }
  188.     if ( win ) CloseWindow( win );
  189.     if ( scr ) CloseScreen( scr );
  190. }
  191.